package cn.edu.nuc.onlinestore.frame;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.server.TCPClient;
import cn.edu.nuc.onlinestore.vo.Cart;
import cn.edu.nuc.onlinestore.vo.Massage;

import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;

public class UserCartFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9199182046183024781L;
	private JPanel contentPane;
	private JTable table;
	private int num=0;
	private JFrame jframe;
	private Cart carts;
	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserCartFrame frame = new UserCartFrame(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 * @param cart 
	 * @param tcp 
	 */
	public UserCartFrame(TCPClient tcp, Cart cart) {
		this.carts=cart;
		this.jframe=this;
		setTitle("购物车详情");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 591, 377);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号(单击排序)");
		model.addColumn("名称");
		model.addColumn("单价(￥)(单击排序)");
		model.addColumn("数量");
		model.addColumn("总价格(￥)");
		//model.addColumn("操作");
		tcp.sendOptmessage(new Massage(0,"","showbuylist",carts));
		Map<String,Goods> shopmap=tcp.getMap();
		Set<String> namelist=shopmap.keySet();
		for(String i:namelist){
			Goods good=shopmap.get(i);
				model.addRow(new String[]{String.valueOf(good.getGid()),good.getGoodsName(),String.valueOf(good.getPrice()),String.valueOf(good.getNum()),String.valueOf(good.getNum()*good.getPrice())});			
			num=num+good.getNum();
		}
		/*model.addRow(new String[]{"1","水杯","15.00","2","30.00"});
		model.addRow(new String[]{"5","农夫山泉","2.00","5","10"});
		model.addRow(new String[]{"6","毛巾","9.90","1","9.90"});*/
		// String columns[] = { "商品编号", "名称","单价(￥)","数量","总价格(￥)"};  
		table = new JTable( model );
		RowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model); 
		table.setRowSorter(sorter);
		//table.setBounds(10, 38, 543, 184);
		
		JScrollPane pane = new JScrollPane( table );
		
		pane.setBounds(20, 20, 500, 200);
		
		tcp.sendOptmessage(new Massage(0,"","totalprice",carts));
		cart=tcp.getTotal();
		contentPane.add( pane );
		JLabel label = new JLabel("总商品数量: "+cart.gettotalnum());
		label.setBounds(195, 232, 116, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("总金额: "+cart.getTotalPrice());
		lblNewLabel.setBounds(342, 232, 211, 15);
		contentPane.add(lblNewLabel);
		
		JButton button = new JButton("结账");
		button.setBounds(456, 283, 93, 23);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, tcp.getmsg());
				}
			}
		);
		contentPane.add(button);

		TextField test = new TextField();
		test.setBounds(386, 253, 60, 20);
		contentPane.add(test);
		
		JButton button2 = new JButton("删除商品");
		button2.setBounds(456, 253, 93, 23);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, tcp.getmsg());
				String name=test.getText();
				if(name!=null&&!"".equals(name.trim())){
					System.out.println("执行按id删除");
			        Pattern pattern = Pattern.compile("[0-9]*"); 
			        Matcher isNum = pattern.matcher(name);
			        int number;
			        if(isNum.matches()){
			            number=Integer.parseInt(name);
			            tcp.sendOptmessage(new Massage(number,"","removeid",carts));
			            if(tcp.getResult().equals("true")){
			            	JOptionPane.showMessageDialog(null, tcp.getmsg());
			            	jframe.setVisible(false);
			            	jframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			            }
			        }
			    else if(namelist.contains(name)){//
			    	System.out.println("执行按名称删除");
			    	tcp.sendOptmessage(new Massage(0,name,"removename",carts));
		            if(tcp.getResult().equals("true")){
		            	JOptionPane.showMessageDialog(null, tcp.getmsg());
		            	jframe.setVisible(false);
		            	jframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		            }
				}else{
					JOptionPane.showMessageDialog(null, "抱歉,输入错误请重新输入");
					test.setText(null);
				}
			}
				contentPane.updateUI();	
		}
	}
);
		contentPane.add(button2);
		
		
	}

}
